package com.softlab.logsystem.service.impl;

import com.softlab.logsystem.common.Page;
import com.softlab.logsystem.common.RestData;
import com.softlab.logsystem.common.exception.LogException;
import com.softlab.logsystem.common.util.PageUtil;
import com.softlab.logsystem.core.mapper.ConsumerMapper;
import com.softlab.logsystem.core.model.LogDto;
import com.softlab.logsystem.core.model.LogVo;
import com.softlab.logsystem.service.ConsumerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LiXiwen
 * @date 2020/1/7 14:06
 */
@Service
public class ConsumerServiceImpl implements ConsumerService {
    private static final Logger logger = LoggerFactory.getLogger(ConsumerServiceImpl.class);

    private final ConsumerMapper consumerMapper;

    @Autowired
    public ConsumerServiceImpl(ConsumerMapper consumerMapper) {
        this.consumerMapper = consumerMapper;
    }

    @Override
    public void addlogDto(LogDto logDto) throws LogException {
        if (null != logDto) {
            if (0 > consumerMapper.addlogDto(logDto)) {
                throw new LogException("Add logDto to MySQL exists Error : ");
            }
        } else {
            throw new LogException("logDto is null!");
        }
    }

    @Override
    public RestData selectDetailByCondition(LogVo logVo) throws LogException {
        Map<String, Object> rtv = new HashMap<>(2);
        /*Page page;
        if (null == logVo.getPage()) {
            logVo.setPage(1);
        }
        page = consumerMapper.countByCondition(logVo);
        page.setNowPage(logVo.getPage());
        page = PageUtil.checkPage(page);*/
        List<LogDto> logDtos = consumerMapper.selectDetailByCondition(logVo);
        //rtv.put("pages", page);
        rtv.put("logs", logDtos);
        return new RestData(rtv);
    }

    @Override
    public RestData selectApps() throws LogException {
        return new RestData(consumerMapper.selectApps());
    }


}
